package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.FancyVideoAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.LifeVideoListDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.PromptPopupWindow;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyReleaseVideoActivity extends BaseActivity {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.prompt_img)
    ImageView promptImg;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;

    PromptPopupWindow promptPopupWindow;
    int position;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fancy_video, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("我发布的视频");
        GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 2);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);


        final ArrayList itemDatas = new ArrayList<>();


        fancyVideoAdapter = new FancyVideoAdapter(this, itemDatas);
        fancyVideoAdapter.setDeleteCallBack(new FancyVideoAdapter.DeleteCallBack() {
            @Override
            public void deleteCallBack(int p) {
                position = p;
                videoId = fancyVideoAdapter.itemDatas.get(position).getId();
                if (promptPopupWindow == null) {
                    promptPopupWindow = new PromptPopupWindow(MyReleaseVideoActivity.this, toolbar);
                    promptPopupWindow.setCallBack(new PromptPopupWindow.CallBack() {
                        @Override
                        public void confirm() {
                            delVideoLife();
//                            delForum();
                        }

                        @Override
                        public void cancle() {

                        }
                    });
                }

                promptPopupWindow.showPopupWindow();
            }
        });


        recycleView.setAdapter(fancyVideoAdapter);


        fancyVideoAdapter.setCustomLoadMoreView(new CustomFootView(this));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);

        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getData(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getData(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });


        getData(true);
    }

    FancyVideoAdapter fancyVideoAdapter;

    int page = 0;

    private void getData(final boolean isRefresh) {

        Map maps = new HashMap();


        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        maps.put("page", page + "");

        maps.put("token", SPUtils.getToken());
        maps.put("userId", SPUtils.getUserID());
        HttpMehtod.getInstance().videoLifeList(maps, new IdeaObserver<BaseDataBean<List<LifeVideoListDataBean>>>() {


            @Override
            public void onSuccess(BaseDataBean<List<LifeVideoListDataBean>> listBaseDataBean) {


                if (isRefresh) {

                    if (xRefreshView == null) {
                        return;
                    }

                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);

                    if (listBaseDataBean.getData() == null) {
                        promptImg.setVisibility(View.VISIBLE);
                        fancyVideoAdapter.notifyDataSetChanged(new ArrayList<LifeVideoListDataBean>());
                        return;
                    } else {
                        promptImg.setVisibility(View.GONE);
                        fancyVideoAdapter.notifyDataSetChanged((ArrayList<LifeVideoListDataBean>) listBaseDataBean.getData());
                    }

                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.setLoadComplete(false);
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    fancyVideoAdapter.addAll(listBaseDataBean.getData());
                }

//
//                if (listBaseDataBean.getData() != null) {
//                    fancyVideoAdapter.notifyDataSetChanged((ArrayList<LifeVideoListDataBean>) listBaseDataBean.getData());
//                    promptImg.setVisibility(View.GONE);
//
//
//                } else {
//                    promptImg.setVisibility(View.VISIBLE);
//
//
//                    fancyVideoAdapter.notifyDataSetChanged(new ArrayList<LifeVideoListDataBean>());
//
//                }


            }

            @Override
            public void onFail(int errorCode) {
                if (xRefreshView != null) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                }
            }

        });

    }

    String videoId = "";

    private void delVideoLife() {
        Map map = new HashMap();


        map.put("videoId", videoId + "");


        HttpMehtod.getInstance().delVideoLife(map, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {

                ToastUtil.show("删除成功");
                fancyVideoAdapter.itemDatas.remove(position);
                fancyVideoAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(int errorCode) {
            }
        });
    }

}
